package com.mall.web.controller;

import com.alibaba.fastjson2.JSON;
import com.mall.base.pojo.CouponsActivity;
import com.mall.base.pojo.CouponsRecord;
import com.mall.web.service.system.CouponsRecordService;
import com.mall.web.service.user.UserService;
import com.mall.web.utils.SnowId;
import com.mall.web.utils.SpringSecurityUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author 游政杰
 */
@Controller
@RequestMapping(path = "/web/couponsRecord")
@Api("优惠券记录 web api")
public class CouponsRecordController {

    private final String DEFAULT_TIME="1970-1-1";

    @Autowired
    private CouponsRecordService couponsRecordService;

    @Autowired
    private SpringSecurityUtil springSecurityUtil;

    @Autowired
    private UserService userService;

    /** 领取优惠券*/
    @ResponseBody
    @GetMapping(path = "/getCoupons",produces = "application/json; charset=utf-8")
    public String getCoupons(long couponsid, HttpSession session){

        String currentUser = springSecurityUtil.currentUser(session);
        long userid = userService.selectUserIdByName(currentUser);

        CouponsRecord couponsRecord = new CouponsRecord();
        couponsRecord.setId(SnowId.nextId());
        CouponsActivity couponsActivity = new CouponsActivity();
        couponsActivity.setId(couponsid);
        couponsRecord.setCouponsActivity(couponsActivity);
        couponsRecord.setUser_id(userid);
        java.util.Date d1 = new java.util.Date();
        Date date = new Date(d1.getTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = simpleDateFormat.format(date);
        couponsRecord.setGetTime(dateString);
        couponsRecord.setUseType(0); //默认未使用
        couponsRecord.setUseTime("");
        String json = couponsRecordService.insertCouponsRecord(couponsRecord);
        return json;
    }

    @ResponseBody
    @GetMapping(path = "/queryCouponsData",produces = "application/json; charset=utf-8")
    public String queryCouponsData(String type,HttpSession session)
    {
        String currentUser = springSecurityUtil.currentUser(session);
        long userid = userService.selectUserIdByName(currentUser);
        int tp = Integer.parseInt(type);
        int page=0;
        int size=8;
        List<CouponsRecord> arrayList = new CopyOnWriteArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime = simpleDateFormat.format(new Date(System.currentTimeMillis()));

        switch (tp)
        {
            case -1: //全部
                arrayList=couponsRecordService.selectCouponsRecordByUserIdAndLimit(page,size,userid,-1,DEFAULT_TIME);
                break;
            case 0: //可使用
                arrayList=couponsRecordService.selectCouponsRecordByUserIdAndLimit(page,size,userid,0,nowtime);
                break;
            case 1: //已使用
                arrayList=couponsRecordService.selectCouponsRecordByUserIdAndLimit(page,size,userid,1,DEFAULT_TIME);
                break;
            default:
                break;
        }

        return JSON.toJSONString(arrayList);
    }



}
